import com.sun.source.tree.Tree;

import java.util.*;

public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public TreeNode createTree1() {

        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');

        C.left = D;
        C.right = E;

        return C;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    // 后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        inOrder(root.right);
        System.out.print(root.val + " ");
    }

    public int nodeSize = 0;

    public int getNoteSize1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        nodeSize++;
        getNoteSize1(root.left);
        getNoteSize1(root.right);
        return nodeSize;
    }

    public int getNoteSize2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return getNoteSize2(root.left) + getNoteSize2(root.right) + 1;
    }

    //为什么不能这样写
    /*public int getNoteSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        getNoteSize(root.left);
        getNoteSize(root.right);
        return nodeSize++;
    }*/

    public int leafCount;

    public int getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        //下面这个if语句是放到递归调节前面还是后面
        /*if (root.left == null && root.right == null) {
            leafCount++;
        }*/
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        if (root.left == null && root.right == null) {
            leafCount++;
        }
        return leafCount;
    }

    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }


    //这样可以吗？
    /*public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left != null && root.right != null) {
            return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
        }
        return 1;
    }*/

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);

        return Math.max(left, right) + 1;
    }

    /**
     * 检测值为value的元素是否存在
     */

    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }

        TreeNode left = find(root.left, val);
        if (left != null) {
            return left;
        }
        TreeNode right = find(root.right, val);
        if (right != null) {
            return right;
        }

        return null;


    }


    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);

        return root;

    }


    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root != null && subRoot == null || root == null && subRoot != null) {
            return false;
        }
        if (root == subRoot) {
            return true;
        }
        if (isSameTree(root, subRoot)) {
            return root.val == subRoot.val;
        }

        return isSubtree(root.left, subRoot.left) || isSubtree(root.right, subRoot.right);
    }

    public TreeNode invertTree1(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null) {
                return true;
            }

            return getHeight(root) >= 0;
        }

        public int getHeight(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int left = getHeight(root.left);
            if (left < 0) {
                return -1;
            }
            int right = getHeight(root.right);

            if (left >= 0 && right >= 0 && Math.abs(left - right) <= 1) {
                return Math.max(left, right) + 1;
            } else {
                return -1;
            }
        }
    }


    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        int left = root.left.val;
        int right = root.right.val;
        if (left == right) {
            return true;
        }
        return isSymmetric(root.left) && isSymmetric(root.right);
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q != null || p != null && q == null) {
            return false;
        }
        if (p == null && q == null) {
            return true;
        }
        if (p.val != q.val) {
            return false;
        }


        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    //层序遍历
    public void levelOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            System.out.print(tmp.val + " ");
            if (tmp.left != null) {
                queue.offer(tmp.left);
            }
            if (tmp.right != null) {
                queue.offer(tmp.right);
            }
        }
    }

    /*public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();

        if (root == null) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            List<Integer> tmplist = new ArrayList<>();
            int size = queue.size();
            while (size != 0) {
                TreeNode cur = queue.poll();
                size--;
                tmplist.add(cur.val);
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(tmplist);
        }

        return ret;
    }*/
    /*public static int i = 0;

    public List<Character> preorderTraversal(TreeNode root) {
        ArrayList<Character> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur!=null) {
                stack.push(cur);
                System.out.print(cur.val+" ");
                ret.add(cur.val);
                cur  = cur.left;
            }
            TreeNode top  = stack.pop();
            cur = top.right;

        }
        return ret;
    }*/

    public List<Character> preorderTraversal(TreeNode root) {
        ArrayList<Character> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                ret.add(cur.val);
                cur = cur.left;
            }
            TreeNode node = stack.pop();
            cur = node.right;
        }
        return ret;
    }

    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder = new StringBuilder();
        tree2strChild(root, stringBuilder);
        return stringBuilder.toString();
    }

    public void tree2strChild(TreeNode root, StringBuilder stringBuilder) {
        if (root == null) return;
        stringBuilder.append(root.val);
        if (root.left != null) {
            stringBuilder.append("(");
            tree2str(root.left);
            stringBuilder.append(")");
        } else {
            if (root.right != null) {
                stringBuilder.append("()");
            } else {
                return;
            }
        }
        if (root.right != null) {
            stringBuilder.append("(");
            tree2str(root.right);
            stringBuilder.append(")");
        } else {
            return;
        }
    }

    public List<Character> inorderTraversal(TreeNode root) {
        ArrayList<Character> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = stack.pop();
            ret.add(tmp.val);
            cur = tmp.right;
        }
        return ret;
    }


    public List<Character> postorderTraversal(TreeNode root) {
        ArrayList<Character> ret = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode tmp = stack.peek();
            if (tmp.right == null || tmp.right == prev) {
                stack.pop();
                ret.add(tmp.val);
                prev = tmp;
            } else {
                cur = tmp.right;
            }
        }
        return ret;
    }


    /*public int sizeIndex = 0;

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChilde(preorder, inorder, 0, inorder.length - 1);
    }

    public TreeNode buildTreeChilde(int[] preorder, int[] inorder, int inbegin, int inend) {
        if (inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[sizeIndex]);
        int rootIndex = findVal(inorder, inbegin, inend, preorder[sizeIndex]);
        sizeIndex++;
        root.left = buildTreeChilde(preorder, inorder, inbegin, rootIndex - 1);
        root.right = buildTreeChilde(preorder, inorder, rootIndex + 1, inend);
        return root;
    }

    public int findVal(int[] inorder, int inbegin, int inend, int key) {
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }*/


    public int size = 0;

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChilde(preorder, inorder, 0, inorder.length - 1);
    }

    public TreeNode buildTreeChilde(int[] preorder, int[] inorder, int inbegin, int inend) {
        if (inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[size]);
        int sizeIndex = finval(inorder, inbegin, inend, preorder[size]);
        size++;
        root.left = buildTreeChilde(preorder, inorder, inbegin, sizeIndex - 1);
        root.left = buildTreeChilde(preorder, inorder, sizeIndex + 1, inend);
        return root;
    }

    public int finval(int[] inorder, int inbegin, int inend, int key) {
        for (int i = inbegin; i <=inend; i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }

}
